#include "rotate.h"
#include <QDebug>

namespace krm {


QMatrix Rotate::save() {

}

QTransform Rotate::rotate(unsigned _angle, bool bula,int pX, int pY) {
    const qreal deg2rad = M_PI/180;
      qreal sina = 0;
      qreal cosa = 0;
      if (_angle == 90. || _angle == -270.)
        sina = 1.;
      else if (_angle == 270. || _angle == -90.)
        sina = -1.;
      else if (_angle == 180.)
        cosa = -1.;
      else{
        qreal b = deg2rad*_angle;            // convert to radians
        sina = sin(b);                   // fast and convenient
        cosa = cos(b);
      }
 /*     qreal tm11 = cosa*mat.m11() + sina*mat.m21();//+mat.dx()*(-pX*cosa-pY*sina+pX);
      qreal tm12 = cosa*mat.m12() + sina*mat.m22();//+mat.dy()*(pX*sina-pY*cosa+pY);
      qreal tm21 = -sina*mat.m11() + cosa*mat.m21();//+mat.dx()*(pX*sina-pY*cosa+pY);
      qreal tm22 = -sina*mat.m12() + cosa*mat.m22();//+mat.dy()*(-pX*cosa-pY*sina+pX);
      qreal l1 = mat.dx();
      qreal l2 = mat.dy();
      QMatrix matt(tm11,tm12,tm21,tm22,l1,l2);

      if(!bula)
        mat = matt;*/
      qreal tm11 = 1;
      qreal tm12 = 0;
      qreal tm13 = pX;
      qreal tm21 = 0;
      qreal tm22 = 1;
      qreal tm23 = pY;

      QTransform trans1(tm11,tm12,tm13,tm21,tm22,tm23,0,0,1);

      tm11 = cosa;
      tm12 = sina;
      tm13 =0;
      tm21 = -sina;
      tm22 = cosa;
      tm23 = 0;

      QTransform trans2(tm11,tm12,tm13,tm21,tm22,tm23,0,0,1);

      tm11 = 1;
      tm12 = 0;
      tm13 = -pX;
      tm21 = 0;
      tm22 = 1;
      tm23 = -pY;

      QTransform trans3(tm11,tm12,tm13,tm21,tm22,tm23,0,0,1);
      trans=trans1*trans2*trans3;
      return trans;
}

}  // end of namespace krm
